import requests
from lxml import etree
if __name__ == "__main__":
    # 爬取页面源码数据
    headers = {
        'user-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
    }
    url = 'https://www.aqistudy.cn/historydata/'
    page_text = requests.get(url=url,headers=headers).text
    #实例化
    tree = etree.HTML(page_text)
    hot_li_list = tree.xpath('//div[@class="bottom"]/ul/li')
    all_city_name = []

    for li in hot_li_list:
        hot_city_name = li.xpath('./a/text()')[0]
        all_city_name.append((hot_city_name))

    #解析的是全部城市名称
    city_names_list = tree.xpath('//div[@class="bottom"]/ul/div[2]/li')
    for li in city_names_list:
        city_name = li.xpath('./a/text()')[0]
        all_city_name.append((city_name))


    print(all_city_name,len(all_city_name))

    #以上方法为热门城市和所有城市分开了2次遍历
    #以下方法为一次过获取全部，通过xpath

   #实则讲解的是一个逻辑运算符“或”运算